package com.zcj.test.gallery;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

public class ImageDetailFragment extends Fragment {
    private static final String IMAGE_DATA_EXTRA = "extra_image_data";
    private static final String TAG = "ImageDetailFragment";
    private String mImageUrl;
    private PhotoView mImageView;
    private ImageFetcher mImageFetcher;
    private PowerImageView gifView;
    private RelativeLayout rlImageDown;

    public static ImageDetailFragment newInstance(String imageUrl) {
        final ImageDetailFragment f = new ImageDetailFragment();
        final Bundle args = new Bundle();
        args.putString(IMAGE_DATA_EXTRA, imageUrl);
        f.setArguments(args);
        return f;
    }

    public ImageDetailFragment() {

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mImageUrl = getArguments() != null ? getArguments().getString(IMAGE_DATA_EXTRA) : null;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Log.d(TAG, "onCreateView: " + mImageUrl);
        final View v = inflater.inflate(R.layout.image_detail_fragment, container, false);
        gifView = (PowerImageView) v.findViewById(R.id.gifview);
        mImageView = (PhotoView) v.findViewById(R.id.imageView);
        mImageView.setTag(mImageUrl);
        rlImageDown = (RelativeLayout) v.findViewById(R.id.rl_iamgedown);
        rlImageDown.setVisibility(View.VISIBLE);
        mImageView.setOnViewTapListener(new PhotoViewAttacher.OnViewTapListener() {
            @Override
            public void onViewTap(View view, float x, float y) {
                ((ImageDetailActivity) getActivity()).chageTitleStatus();
            }
        });
        gifView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "onClick: ");
                ((ImageDetailActivity) getActivity()).chageTitleStatus();
            }
        });
        gifView.setOnFinishListener(new PowerImageView.OnFinish() {
            @Override
            public void onFinish() {
                rlImageDown.setVisibility(View.GONE);
            }
        });
        return v;
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Log.d(TAG, "onActivityCreated: " + mImageUrl);
        if (mImageUrl.toLowerCase().endsWith("gif")) {
            gifView.notifiChange(mImageUrl);
            gifView.setVisibility(View.VISIBLE);
            mImageView.setVisibility(View.GONE);
        } else {
            gifView.setVisibility(View.GONE);
            mImageView.setVisibility(View.VISIBLE);
            if (ImageDetailActivity.class.isInstance(getActivity())) {
                mImageFetcher = ((ImageDetailActivity) getActivity()).getImageFetcher();
                Handler handler = new Handler(new Handler.Callback() {
                    @Override
                    public boolean handleMessage(Message msg) {
                        if (msg.what == ImageWorker.MESSAGE_FAILED) {
                            mImageView.setImageResource(R.mipmap.ic_bad_pic);
                        } else if (msg.what == ImageWorker.MESSAGE_FINASH) {
                            rlImageDown.setVisibility(View.GONE);
                        }
                        rlImageDown.setVisibility(View.GONE);
                        return true;
                    }
                });
                mImageFetcher.loadImage(mImageUrl, mImageView, handler);
            }
        }

    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy: " + mImageUrl);
        if (mImageView != null) {
            ImageWorker.cancelWork(mImageView);
            mImageView.setImageDrawable(null);
        }
    }
}
